package com.ecommerce.dal.dao.impl;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.ecommerce.dal.dao.ICustomerItemDAO;
import com.ecommerce.dal.entity.Customer;
import com.ecommerce.dal.entity.CustomerItem;
import com.ecommerce.dal.util.CustomHibernateDAOSupport;

@Repository("customerItemDAO")
public class CustomerItemDAO extends CustomHibernateDAOSupport implements
		ICustomerItemDAO {

	@Override
	public void save(CustomerItem customerItem) {
		getHibernateTemplate().save(customerItem);

	}

	@Override
	public void delete(CustomerItem customerItem) {
		getHibernateTemplate().delete(customerItem);

	}

	@Override
	public List<CustomerItem> findByCustomerId(long customerId) {
		List list = getHibernateTemplate()
				.find("select customerItem from CustomerItem customerItem inner join customerItem.customer customer inner join customer.user user where user.userId=?",
						customerId);
		return ((list != null && list.size()>0) ?  list : null) ;
	}

	@Override
	public CustomerItem findByVerificationAndId(long customerId,
			long verification) {
		List list = getHibernateTemplate()
				.find("select customerItem from CustomerItem customerItem inner join customerItem.customer customer inner join customer.user user where user.userId = ? and customerItem.verification=?",
						customerId, verification);
		return (CustomerItem) ((list != null && list.size()>0) ?  list.get(0) : null) ;
	}

	@Override
	public CustomerItem findByVerificationAndUserName(String customerUserName,
			long verification) {
		List list = getHibernateTemplate()
				.find("select customerItem from CustomerItem customerItem inner join customerItem.customer customer inner join customer.user user where  user.userName=? and customerItem.verification=?",
						 customerUserName, verification);
		return (CustomerItem) ((list != null && list.size()>0) ?  list.get(0) : null) ;
	}

	@Override
	public CustomerItem findByVerification(long verification) {
		List list = getHibernateTemplate()
				.find("from CustomerItem customerItem where customerItem.verification=?",
						verification);
		return (CustomerItem) ((list != null && list.size()>0) ?  list.get(0) : null) ;
	}

	@Override
	public Customer findCustomerByVerification(long verification) {
		List list = getHibernateTemplate()
				.find("select customer from CustomerItem customerItem inner join customerItem.customer customer where customerItem.verification = ?",
						verification);
		return (Customer) ((list != null && list.size()>0) ?  list.get(0) : null) ;
	}
}
